package com.walter.controller;

import com.walter.annotation.SysLog;
import com.walter.common.BaseController;
import com.walter.common.CommonQuery;
import com.walter.common.Result;
import com.walter.entity.Role;
import com.walter.model.form.SaveRoleForm;
import com.walter.model.vo.RoleVO;
import com.walter.service.RoleService;
import com.walter.util.ValidationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：ch
 * @date ：Created in 2021/5/25
 * @description：
 * @modified By：
 * @version: 1.0
 */
@Controller
@RequestMapping("/api/role")
@Api(tags = "角色管理")
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;

    @PostMapping("save")
    @ResponseBody
    @ApiOperation("后管 添加/修改角色")
    @SysLog("后管 添加/修改角色")
    public Result<Boolean> save(@RequestBody SaveRoleForm form){
        ValidationUtils.validateEntity(form);
        return success(roleService.save(form));
    }

    @PostMapping("findList")
    @ResponseBody
    @ApiOperation("后管 无分页查询所有角色(可按中英文名称模糊查询)")
    public Result<List<Role>> findList(@RequestBody(required = false) CommonQuery query){
        return success(roleService.findList(query));
    }

    @GetMapping("deleteRelation/{roleId}")
    @ResponseBody
    @ApiOperation("后管 删除单个角色(包括关联表数据)")
    @SysLog("后管 删除单个角色")
    public Result<Integer> deleteRelation(@PathVariable Integer roleId){
        return success(roleService.deleteRelation(roleId));
    }

    @GetMapping("findRoleDetail/{roleId}")
    @ResponseBody
    @ApiOperation("后管 查角色详情")
    public Result<RoleVO> findRoleDetail(@PathVariable Integer roleId){
        return success(roleService.findRoleDetail(roleId));
    }

    @GetMapping("findRoleIdsByUserId/{userId}")
    @ResponseBody
    @ApiOperation("后管 根据用户id查询角色id列表")
    public Result<List<Integer>> findRoleIdsByUserId(@PathVariable Integer userId){
        return success(roleService.findRoleIdsByUserId(userId));
    }
}
